<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rules"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input
        v-model="userInfo.password"
        type="password"
        style="width: 300px"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserByIdDetail } from "@/api/user";
import { saveUserDetailById } from '@/api/employees';
export default {
  data() {
    return {
      id: this.$route.query.userId,
      userInfo: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created(){
    this.loadUserInfo()
  },
  methods: {
    //修改员工姓名和手机号
    async updateUserInfo(){
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('saveUserDetailById', res)
        this.$router.back()
      } catch (err) {
        console.log('saveUserDetailById', err)
      }
    },
    hSubmit() {
      this.$refs.userForm.validate((valid) => {
        if (!valid) return;
        this.updateUserInfo()
      });
    },
    async loadUserInfo() {
      try {
        const res = await getUserByIdDetail(this.id);
        // console.log("getUserByIdDetail", res);
        if(res.code!==10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.userInfo=res.data
      } catch (err) {
        console.log("getUserByIdDetail", err);
      }
    },
  },
};
</script>
		